package com.pm.service.impl;

import com.pm.entity.Project;
import com.pm.mapper.ProjectMapper;
import com.pm.service.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 项目服务实现类
 */
@Service
public class ProjectServiceImpl implements ProjectService {
    
    @Autowired
    private ProjectMapper projectMapper;
    
    @Override
    public Project createProject(Project project) {
        project.setCreatedAt(LocalDateTime.now().toString());
        project.setUpdatedAt(LocalDateTime.now().toString());
        if (project.getStatus() == null) {
            project.setStatus("PLANNING");
        }
        projectMapper.insert(project);
        return project;
    }
    
    @Override
    public Project updateProject(Project project) {
        project.setUpdatedAt(LocalDateTime.now().toString());
        projectMapper.updateById(project);
        return project;
    }
    
    @Override
    public boolean deleteProject(String id) {
        return projectMapper.deleteById(id) > 0;
    }
    
    @Override
    public Project findById(String id) {
        return projectMapper.selectById(id);
    }
    
    @Override
    public List<Project> getAllProjects() {
        return projectMapper.selectList(null);
    }
    
    @Override
    public List<Project> findByManagerId(String managerId) {
        return projectMapper.findByManagerId(managerId);
    }
    
    @Override
    public List<Project> findByStatus(String status) {
        return projectMapper.findByStatus(status);
    }
    
    @Override
    public boolean updateProjectStatus(String id, String status) {
        Project project = findById(id);
        if (project == null) {
            throw new RuntimeException("项目不存在");
        }
        
        project.setStatus(status);
        project.setUpdatedAt(LocalDateTime.now().toString());
        
        return projectMapper.updateById(project) > 0;
    }
}